#include "UnitTest++.h"
#include "PageBuilder.h"
#include <string>
#include <iostream>

TEST(ShouldSeparateATextFileInPages)
{
    // arrange
    PageBuilder builder;

    Page expectedPage(1, "Patterns and Antipatterns\n"
            "One of the ways humans solve newly encountered problems is by subconsciously\n"
            "applying a previously successful solution to a similar or related\n"
            "problem. This approach to problem solving is variously known as analogical,\n"
            "allegorical, or case-based reasoning and is a well-known machinelearning\n"
            "technique used in artificial intelligence systems.\n");

    // act
    builder.open("test/resources/book1.txt");

    try
    {
        Page page = builder.nextPage();
        //assert
        CHECK_EQUAL(expectedPage, page);
    }catch(std::exception& e)
    {
        CHECK(false);
        std::cout << e.what();
    }
}

TEST(ShouldSeparateATextContainingTwoPages)
{
    //arrange
    PageBuilder builder;
    Page expectedFirstPage(1, "Patterns and Antipatterns\n"
            "One of the ways humans solve newly encountered problems is by subconsciously\n"
            "applying a previously successful solution to a similar or related\n"
            "problem. This approach to problem solving is variously known as analogical,\n"
            "allegorical, or case-based reasoning and is a well-known machinelearning\n"
            "technique used in artificial intelligence systems.\n");

    Page expectedSecondPage(2, "And what if you are just starting out in your career or are a newly\n"
            "appointed manager? Where does your experience base come from?\n"
            "What if, however, we could capture your experiences and those of\n"
            "others  successes and failures  in solving problems? Experts could\n"
            "then share those experiences with each other and, more importantly, with\n"
            "their less-experienced colleagues.\n");

    // act
    builder.open("test/resources/book2.txt");

    try
    {
        Page page = builder.nextPage();
        Page page2 = builder.nextPage();
        //assert
        CHECK_EQUAL(expectedFirstPage, page);
        CHECK_EQUAL(expectedSecondPage, page2);
    }catch(std::exception& e)
    {
        CHECK(false);
        std::cout << e.what();
    }
}

TEST(ShouldSetPageNumberToTheRespectivePage)
{
    //arrange
    PageBuilder builder;

    // act
    builder.open("test/resources/book2.txt");
    int expectedPageNumber = 1;

    while(builder.hasNext())
    {
        Page page = builder.nextPage();
        //assert
        CHECK_EQUAL(expectedPageNumber, page.number_);
        expectedPageNumber++;
    }
}

TEST(ShouldReadATextFileWithAllPages)
{
    //arrange
    PageBuilder builder;
    int pageCount = 0;
    int expectedPageNumber = 2;

    // act
    builder.open("test/resources/book2.txt");

    while(builder.hasNext())
    {
        builder.nextPage();
        pageCount++;
    }

    //assert
    CHECK_EQUAL(expectedPageNumber, pageCount);
}

TEST(ShouldReadATextFileWithTheLastPageDoesntHavePageNumber)
{
    //arrange
    PageBuilder builder;
    int pageCount = 0;
    int expectedPageNumber = 3;

    // act
    builder.open("test/resources/book3.txt");

    while(builder.hasNext())
    {
        builder.nextPage();
        pageCount++;
    }

    //assert
    CHECK_EQUAL(expectedPageNumber, pageCount);
}